<?php
/**
 * Created by PhpStorm.
 * User: Giles
 * Date: 2017/7/27
 * Time: 15:29
 */
namespace Admin\Controller;


class AgentController extends BaseController
{
    /**
     * 列表
     *
     */
    public function index()
    {
        $this->display();
    }

    public function ajaxGetIndex() {
        $postData = I('post.');
        $start = $postData['start'] ? $postData['start'] : 0;
        $limit = $postData['length'] ? $postData['length'] : 20;
        $draw = $postData['draw'];
        $where = array();
        $getInfo = I('get.');
        if (isset($getInfo['name']) && !empty($getInfo['name'])) {
            $where['name'] = urldecode($getInfo['name']);
        }
        $total = D('ApiAgent')->where($where)->count();
        $info = D('ApiAgent')->where($where)->limit($start, $limit)->select();
        //echo D('ApiOrders')->getLastSql();
        $data = array(
            'draw'            => $draw,
            'recordsTotal'    => $total,
            'recordsFiltered' => $total,
            'data'            => $info
        );
        $this->ajaxReturn($data, 'json');
    }

    public function add()
    {
        $getId = I('get.id');
        if (IS_POST) {
            $data = I('post.');
            if(isset($data['id']) && !empty($data['id'])) {
                $save = $data;
                unset($save['id']);
                $res = D('ApiAgent')->where(array('id' => $data['id']))->save($data);
            } else {
                $res = D('ApiAgent')->add($data);
            }

            if ($res === false) {
                $this->ajaxError('操作失败');
            } else {
                $this->ajaxSuccess('添加成功');
            }
        } else {
            $detale = D('ApiAgent')->where(array('id' => $getId))->find();
            $this->assign('detail', $detale);
            $this->display();
        }
    }
}